package com.ihd.ihardware.view.tzc.discovery.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;

import com.ihd.ihardware.R;
import com.ihd.ihardware.databinding.DiscoveryFragmentBinding;
import com.ihd.ihardware.view.tzc.main.view.BalanceActivity;
import com.ihd.ihardware.view.tzc.main.viewmodel.BalanceViewModel;
import com.trello.rxlifecycle2.components.support.RxFragment;

import java.util.ArrayList;
import java.util.List;

import cn.wowjoy.commonlibrary.base.BaseFragment;

public class DiscoveryFragment extends BaseFragment<DiscoveryFragmentBinding, BalanceViewModel> {
    private boolean isVisible = false;
    private FragmentPagerAdapter mFragmentPagerAdapter;
    private List<BaseFragment> mFragment;

    public DiscoveryFragment() {
    }

    public static DiscoveryFragment newInstance(String param1, String param2) {
        DiscoveryFragment fragment = new DiscoveryFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }

        mFragment = new ArrayList<>();
        mFragment.add(GZFragment.newInstance("", ""));
        mFragment.add(FXFragment.newInstance("", ""));
        mFragment.add(MJFragment.newInstance("", ""));
        mFragmentPagerAdapter = new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public RxFragment getItem(int position) {
                return mFragment.get(position);
            }

            @Override
            public int getCount() {
                return mFragment.size();
            }
        };
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        if (isVisible)
            ((BalanceActivity) getActivity()).mImmersionBar.statusBarDarkFont(true).init();
        binding.vp.setAdapter(mFragmentPagerAdapter);
        binding.vp.setCurrentItem(0);

        binding.gzLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.gzTV.setTextColor(getResources().getColor(R.color.C_32353B));
                binding.gzV.setVisibility(View.VISIBLE);
                binding.fxTV.setTextColor(getResources().getColor(R.color.C_B5B7BF));
                binding.fxV.setVisibility(View.INVISIBLE);
                binding.mjTV.setTextColor(getResources().getColor(R.color.C_B5B7BF));
                binding.mjV.setVisibility(View.INVISIBLE);
                binding.vp.setCurrentItem(0, true);
            }
        });

        binding.fxLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.gzTV.setTextColor(getResources().getColor(R.color.C_B5B7BF));
                binding.gzV.setVisibility(View.INVISIBLE);
                binding.fxTV.setTextColor(getResources().getColor(R.color.C_32353B));
                binding.fxV.setVisibility(View.VISIBLE);
                binding.mjTV.setTextColor(getResources().getColor(R.color.C_B5B7BF));
                binding.mjV.setVisibility(View.INVISIBLE);
                binding.vp.setCurrentItem(1, true);
            }
        });

        binding.mjLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.gzTV.setTextColor(getResources().getColor(R.color.C_B5B7BF));
                binding.gzV.setVisibility(View.INVISIBLE);
                binding.fxTV.setTextColor(getResources().getColor(R.color.C_B5B7BF));
                binding.fxV.setVisibility(View.INVISIBLE);
                binding.mjTV.setTextColor(getResources().getColor(R.color.C_32353B));
                binding.mjV.setVisibility(View.VISIBLE);
                binding.vp.setCurrentItem(2, true);
            }
        });

        binding.msgIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), MsgActivity.class);
                startActivity(i);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.discovery_fragment;
    }

    @Override
    protected Class<BalanceViewModel> getViewModelClass() {
        return BalanceViewModel.class;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        if (isVisibleToUser && getActivity() != null)
            ((BalanceActivity) getActivity()).mImmersionBar.statusBarDarkFont(true).init();
    }
}
